import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt #其中有许多常量
 
 
class QSliderDemo(QWidget):
    def __init__(self):
        super(QSliderDemo,self).__init__()
        self.initUI()
 
 
    def initUI(self):
        self.setWindowTitle('QSliderDemo')
        self.resize(350,500)
        #垂直布局
        layout=QVBoxLayout()
 
        self.label=QLabel('Hello PyQt5!')
        self.label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.label)
 
        #滑块分为水平和垂直两种
        #水平slider1
        self.slider1=QSlider(Qt.Horizontal)#水平，左右滑动
        #设置最小值
        self.slider1.setMinimum(12)
        # 设置最大值
        self.slider1.setMaximum(48)
        #步长
        self.slider1.setSingleStep(3)
        #设置当前值
        self.slider1.setValue(18)
        #设置刻度的位置，刻度在下方
        self.slider1.setTickPosition(QSlider.TicksBelow)
        #刻度间隔
        self.slider1.setTickInterval(6)
        self.slider1.valueChanged.connect(self.valueChange)
 
        #垂直slider2
        self.slider2=QSlider(Qt.Vertical) #垂直滑块
        self.slider2.setMinimum(1)
        self.slider2.setMaximum(80)
        self.slider2.setSingleStep(5)
        self.slider2.setTickPosition(QSlider.TicksLeft)#刻度条放在滑块的左边
        self.slider2.setTickInterval(10)
        self.slider2.valueChanged.connect(self.valueChange)
 
        layout.addWidget(self.slider1)
        layout.addWidget(self.slider2)
        self.setLayout(layout)
 
 
    def valueChange(self):
        '''
        注意这里是sender(),而不是slider1也不是slider2,sender()获取当前操作的控件，
        这样无论拖动哪个滑动条字体大小都会变化，因为这俩信号对应这一个槽函数
        '''
        print('当前值：%s'% self.sender().value())
        size=self.sender().value() #获得当前值
        #使字号根据当前值来变化
        self.label.setFont(QFont('Arial',size))
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=QSliderDemo()
    main.show()
    sys.exit(app.exec_())
